/*
=========== 插件可设置参数及默认值 ===========
onOff:true, //默认开启,此项一般不作设置
width:"", //宽度
height:"", //高度
cover:false, //是否遮罩
coverDiv:"floatDivCover", //遮罩层挂勾(填写元素id，此项一般不作设置)
coverColor:"#000000", //遮罩背景颜色
fade:50, //遮罩透明度
space:0, //沿边间距
level:"center", //水平排列方式(left,center,right)
vertical:"middle", //竖直排列方式(top,middle,bottom)
closeHandler:"" //关闭句柄(填写元素id)
====================================
插件版本：V 1.0
@作者:路卡卡
QQ:1599538531
2011-12-11
====================================
PS:
	同时解决在万恶的IE6下两个问题：select不能遮住问题；改善滚动时层抖动问题(还有轻微，日后找到更好的办法再更新);
	支持ESC键关闭;
注意：
	1、如果浮动层里是ajax动态加载内容且层未定宽高，请在ajax回调函数里取得数据之后的位置调用本插件方法，否则层位置可能产生偏差；
	2、如果在自定义方法执行过程中需要关闭请使用$("selector").floatDiv({onOff:false})，如ajax的回调。其它关闭如单击关闭设置closeHandler即可；
====================================
*/
;(function($) {
	$.fn.extend({
		floatDiv: function(opt) {
			opt = $.extend({
				onOff: true,
				width: "",
				height: "",
				cover: false,						
				coverDiv: "floatDivCover",
				coverColor: "#000000",
				fade: 50,
				space: 0,
				level: "center",
				vertical: "middle",
				closeHandler: ""
			},opt);
			var obj = $(this),
			selfWidth,
			selfHeight,
			maxZ = 0,
			browserVersion,
			frame = "";
			var init = function() {
				if ($("#floatDivTemp").length == 0) {
					obj.append("<input type='hidden' value='' id='floatDivTemp' />")
				};
				if (navigator.userAgent.indexOf("MSIE 6.0") != -1) {
					if ($("#floatDivTemp").val() == "") {
						$("head").append("<style>html{ background-image:url(about:blank); background-attachment:fixed;}</style>");
						$("body").append('<iframe id="framezd" scrolling="no" frameborder="0"></iframe>');
						$("#floatDivTemp").val(opt.coverDiv)
					};
					frame = opt.cover == true ? "zdCover": "zdDiv";
					browserVersion = "ie6";
				};
				if ($("#floatDivTemp").val() == "") {
					$("#floatDivTemp").val(opt.coverDiv)
				};
				maxZ = getMaxZindex();
				if (opt.closeHandler != '') {
					$("#" + opt.closeHandler).click(function() {
						closeEvent();
					})
				};
				selfWidth = opt.width == "" ? obj.width() : opt.width;
				selfHeight = opt.height == "" ? obj.height() : opt.height;
				obj.width(selfWidth).css({"position": "fixed"}).slideDown();
				if (browserVersion == "ie6") {
					obj.css("position", "absolute");
				};
				if (opt.cover) {
					if ($("#" + opt.coverDiv).length == 0) {
						$("body").append("<div id=" + opt.coverDiv + "></div>");
					};
					coverFix()
				} else {
					if (browserVersion == "ie6") {
						$("#framezd").width(selfWidth).height(selfHeight).css({
							"position": "absolute",
							"left": "0px",
							"top": "0px",
							"z-index": maxZ + 10,
							"opacity": 0,
							"filter": "alpha(opacity=0)"
						}).show();
						frame = "zdDiv";
					}
				};
				keepfix(opt.level, opt.vertical);
			};
			var coverFix = function() {
				var doc = document.compatMode ? document.documentElement: document.body,
				scrollWidth = doc.scrollWidth,
				scrollHeight = doc.scrollHeight;
				$("#" + opt.coverDiv).width(scrollWidth).height(scrollHeight).css({
					"position": "absolute",
					"left": "0px",
					"top": "0px",
					"display": "none",
					"background": opt.coverColor,
					"opacity": opt.fade / 100,
					"filter": "alpha(opacity=" + opt.fade + ")",
					"z-index": maxZ + 2
				}).show();
				if (browserVersion == "ie6") {
					$("#framezd").width(scrollWidth).height(scrollHeight).css({
						"position": "absolute",
						"left": "0px",
						"top": "0px",
						"opacity": 0,
						"filter": "alpha(opacity=0)",
						"z-index": maxZ + 10
					}).show();
					frame = "zdCover";
				}
			};
			var keepfix = function(level, vertical) {
				var winWidth = $(window).width(),
				winHeight = $(window).height(),
				scrollLeft = $(window).scrollLeft(),
				scrollTop = $(window).scrollTop(),
				_left_l = opt.space,
				_left_c = (winWidth - selfWidth) / 2,
				_left_r = winWidth - selfWidth - opt.space,
				_top_t = opt.space,
				_top_m = (winHeight - selfHeight) / 2,
				_top_b = winHeight - selfHeight - opt.space;
				_left = level == "left" ? _left_l: (level == "center" ? _left_c: _left_r);
				_top = vertical == "top" ? _top_t: (vertical == "middle" ? _top_m: _top_b);
				if (browserVersion == "ie6") {
					_left += scrollLeft;
					_top += scrollTop;
					if (frame == "zdDiv") {
						if(_top<0){
							_top = 0;
							$("#framezd").css('position','absolute');
						}
						$("#framezd").css({
							"z-index": maxZ + 10,
							"left": _left + "px",
							"top": _top + "px"
						})
						if(selfHeight>winHeight){
							obj.css({'position':'absolute','top':$(window).scrollTop()});
							$(window).scroll(function(){
								if($(window).scrollTop()<obj.position().top){
								   $(window).scrollTop(obj.position().top);
								}
								if($(window).scrollTop()>obj.position().top+selfHeight-winHeight){
									$(window).scrollTop(obj.position().top+selfHeight-winHeight+1);
								}
							});
						}

					}
				};
				if(_top<0){
					_top = 0;
					obj.css('position','absolute');
				}
				obj.css({
					"z-index": maxZ + 30,
					"left": _left + "px",
					"top": _top + "px"
				});
				if(selfHeight>winHeight){
					obj.css({'position':'absolute','top':$(window).scrollTop()});
					$(window).scroll(function(){
//                        obj.css({'position':'absolute','top':$(window).scrollTop()});
						if($(window).scrollTop()<obj.position().top){
                            obj.css({'position':'absolute','top':$(window).scrollTop()});
//							$(window).scrollTop(obj.position().top);
						}
						if($(window).scrollTop()>obj.position().top+obj.height()-$(window).height()){
                            obj.css({'position':'absolute','top':$(window).scrollTop()-(obj.height()-winHeight)});
//							$(window).scrollTop(obj.position().top+obj.height()-$(window).height()+1);
						}
					});
				}
			};
			var closeEvent = function() {
				obj.css("z-index", 0).slideUp();
				$(window).unbind();
				if ($("#" + $("#floatDivTemp").val()).length != 0) {
					$("#" + $("#floatDivTemp").val()).css("z-index", 0).hide()
				};
				if ($("#framezd").length != 0) {
					$("#framezd").css("z-index", 0).hide()
				};
			};
			var getMaxZindex = function() {
				return 100000;
			};
			if (opt.onOff) {
				$(window).bind("scroll resize",function(){
					//keepfix(opt.level, opt.vertical);
				});
				$(document).bind("keyup",function(e) {
					if (e.which == 27 && obj.is(":visible")) {
						closeEvent();
					}
				});
				init();
			} else {
				closeEvent();
			}
		}
	})
})(jQuery);